package com.cian.service.impl;

import com.cian.entity.CommodityInform;
import com.cian.entity.dto.CommodityDTO;
import com.cian.entity.dto.StockAddDTO;
import com.cian.mapper.CommodityInformMapper;
import com.cian.service.ICommodityInformService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 商品详细信息表 服务实现类
 * </p>
 *
 * @author Cian
 * @since 2021-03-07
 */
@Service
public class CommodityInformServiceImpl extends ServiceImpl<CommodityInformMapper, CommodityInform> implements ICommodityInformService {
    @ApiModelProperty("表示查询的时候一页最大的数量")
    private static final int MAX_PAGE_NUMBER = 5;

    @Autowired
    CommodityInformMapper commodityInformMapper;

    public void insert(CommodityInform commodityInform){
        commodityInformMapper.insert(commodityInform);
    }

    @Override
    public List<CommodityDTO> selectAll(int nextPage) {
        List<CommodityInform> commodityInforms =
                commodityInformMapper.selectByCondition("0",null, MAX_PAGE_NUMBER, nextPage);
        if(commodityInforms == null || commodityInforms.isEmpty()) return null;
        Integer count = commodityInformMapper.countByCondition("0",null);
        // 不会使用到多线程，为了效率使用LinkList
        LinkedList<CommodityDTO> linkedList = new LinkedList<>();
        commodityInforms.forEach(index -> {linkedList.add(new CommodityDTO(count, MAX_PAGE_NUMBER, index));});
        return linkedList;
    }

    @Override
    public List<CommodityDTO> selectByCondition(String select, String condition, int nextPage) {
        List<CommodityInform> commodityInforms =
                commodityInformMapper.selectByCondition(select, condition,MAX_PAGE_NUMBER, nextPage);
        if(commodityInforms == null || commodityInforms.isEmpty()) return null;
        Integer count = commodityInformMapper.countByCondition(select,condition);
        LinkedList<CommodityDTO> linkedList = new LinkedList<>();
        commodityInforms.forEach(index -> linkedList.add(new CommodityDTO(count, MAX_PAGE_NUMBER, index)));
        return linkedList;
    }

    @Override
    public Boolean delete(int id) {
        return commodityInformMapper.deleteById(id) != 0;
    }

    @Override
    public List<StockAddDTO> selectStockAddInfrom() {
        return commodityInformMapper.selectStockAddInfrom();
    }


}
